from machine import SPI, Pin

sck = Pin(14)
mosi = Pin(13)

spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=sck, mosi=mosi)
cs = Pin(27, Pin.OUT)

def max7219(reg, data):
	global spi, cs

	cs.value(0)
	# for count in range(4):
	spi.write(bytearray([reg, data]))
	cs.value(1)

def write_byte(data):
	global sck, mosi, cs

	for i in range(8):
		sck.value(0)
		mosi.value(data&0x80)
		data <<= 1
		sck.value(1)

def write_data(addr,dat):
	global cs

	cs.value(0)
	for i in range(1):
		write_byte(addr)
		write_byte(dat)
	cs.value(1)

def show(reg, data):
	write_data(reg, data)

# def show(reg, data):
# 	global spi, cs


# 	# for y in range(8):
# 	cs.value(0)
# 	for count in range(1):
# 		spi.write(bytearray([reg, data]))
# 	cs.value(1)

	# print(bytearray([reg, data]))

max7219(0x0b, 7)
max7219(0x09, 0)
max7219(0x0a, 15)
max7219(0x0f, 0)
max7219(0x0c, 1)

symbol = [0x60, 0xf0, 0xf0, 0x7f, 0x07, 0x06, 0x0c, 0x08]
# symbol = [0x06, 0x60, 0x08, 0x60, 0x60, 0x60, 0x60, 0x60]

for i in range(8):
	max7219(i + 1, 1)

for i in range(8):
	max7219(i + 1, symbol[i])
	# print(bytearray([i + 1, symbol[i]]))

# max7219(0x0a, 0)
